package com.sd.controller;

import com.sd.entity.BusinessHospital;
import com.sd.entity.Result;
import com.sd.entity.ReturnBean;
import com.sd.service.BusinessHospitalService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (BusinessHospital)表控制层
 *
 * @author makejava
 * @since 2021-06-15 10:46:04
 */
@Controller
@RequestMapping("Business")
public class BusinessHospitalController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessHospitalService businessHospitalService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BusinessHospital selectOne(Integer id) {
        return this.businessHospitalService.queryById(id);
    }



    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 去显示所有商品的页面
     * @create time: 2021/4/22 9:18
     * @return
     */
    @RequestMapping("toShowBusinessPage")
    public String toShowBusinessPage(){
        return "showBusiness";
    }
    @RequestMapping("toAddBusinessPage")
    public String toAddBusinessPage(){
        return "addBusiness";
    }

    @RequestMapping("findAllBusiness")
    //返回json数据
    @ResponseBody
    public ReturnBean findAllBusiness(int page, int limit){
        //page当前第几页
        //limit一页显示多少行
        int  offset=(page-1)*limit;
        //int offset 起始位置, int limit，长度，共几行，一页显示多少行
        List<BusinessHospital> BusinessList = businessHospitalService.queryAllByLimit(offset, limit);
        ReturnBean table = new ReturnBean();
        table.setCode(0);
        //返回所有的商品条数
        Map<String, Long> map = businessHospitalService.getCount();
        //从map集合中获取key为myCount的value值
        Long myCount = map.get("myCount");
        table.setCount( myCount);
        table.setData(BusinessList);
        return table;
    }
    @RequestMapping("findAllBusinessNoPage")
    @ResponseBody
    public List<BusinessHospital> findAllBusinessNoPage(){
        //int offset 起始位置, int limit，长度，共几行，一页显示多少行
        List<BusinessHospital> BusinessList = businessHospitalService.queryAll(null);
        return BusinessList;
    }

    @RequestMapping("saveBusiness")
    @ResponseBody
    public Result saveBusiness(BusinessHospital Business){
        Result result= new Result();
        try {
            Business.setCreateTime(new Date());
            businessHospitalService.insert(Business);
            result.setCode(0);
            result.setMsg("添加成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("添加失败");
        }
        return result;
    }

    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 修改学生
     * @create time: 2021/5/18 9:10
     * @return
     */
    @RequestMapping("updateBusiness")
    @ResponseBody
    public Result updateBusiness( BusinessHospital Business){
        Result result= new Result();
        try {
            businessHospitalService.update(Business);
            result.setCode(0);
            result.setMsg("修改成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("修改失败");
        }
        return result;
    }
    @RequestMapping("deleteBusinessById")
    @ResponseBody
    public Result deleteBusinessById( int id){
        Result result= new Result();
        try {
            businessHospitalService.deleteById(id);
            result.setCode(0);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("删除失败");
        }
        return result;
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 按照id修改学生
     * @create time: 2021/4/29 16:17
     * @return
     */
    @RequestMapping("deleteBusinessByIds")
    @ResponseBody
    /**RequestBody将json字符串转换成java对象*/
    public Result deleteBusinessByIds( @RequestBody List<BusinessHospital> BusinessList){
        Result returnEntity =new Result();
        boolean b = businessHospitalService.deleteBusinessByIds(BusinessList);
        if(b){
            returnEntity.setCode(0);
            returnEntity.setMsg("批量删除成功");
        }else{
            returnEntity.setCode(1);
            returnEntity.setMsg("批量删除失败");
        }
        return returnEntity;
    }

}